// gui
import { useEffect } from 'react'
import * as THREE from 'three'
import './App.css'
// 导入轨道控制器
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'

import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js'
// 导入网格帮助其

function App() {

    useEffect(() => {
        // 场景
        const scene = new THREE.Scene()
        // 创建相机
        const camera = new THREE.PerspectiveCamera(
            45,//相机的角度
            window.innerWidth / window.innerHeight,//相机的宽高比
            0.1,//相机的近截面
            1000//相机的远截面
        )

        // 创建渲染器
        const renderer = new THREE.WebGLRenderer()
        renderer.setSize(window.innerWidth, window.innerHeight)//设置渲染尺寸
        document.body.appendChild(renderer.domElement)//将渲染器添加到文档中

        // 创建网格帮助器
        const gridHelper = new THREE.GridHelper(100, 50)
        scene.add(gridHelper)
        // --------------------------------main--------------------------------------------
        // 创建几何体
        // 要更新旋转角度的对象数组
        const objects = [];

        // 创建小熊坐标系
        const bearObrit = new THREE.Object3D()
      
        // 添加gridhelper
        const bearGridHelper = new THREE.GridHelper(10, 100)
        bearObrit.add(bearGridHelper)
        bearObrit.position.set(0, 1, 0)
        scene.add(bearObrit)
        // 创建小熊
        const bearGeometry = new THREE.BoxGeometry(1, 1, 1)
        const bearMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 })
        const bearMesh = new THREE.Mesh(bearGeometry, bearMaterial)
        bearObrit.add(bearMesh)


        // 创建气球坐标系
        const balloonObrit = new THREE.Object3D()
        balloonObrit.position.y = 2
        // 添加gridhelper
        const balloonGridHelper = new THREE.GridHelper(10, 100)
        balloonObrit.add(balloonGridHelper)
        // 添加气球坐标系到小熊坐标系
        bearObrit.add(balloonObrit)
        // 将小熊坐标系添加到数组 
        objects.push(bearObrit)
        // 创建气球
        const balloonGeometry = new THREE.SphereGeometry(1, 32, 32)
        const balloonMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 })
        const balloonMesh = new THREE.Mesh(balloonGeometry, balloonMaterial)
        balloonObrit.add(balloonMesh)
      
        const color = 0xffffff;
        const intensity = 500;
        const light = new THREE.PointLight(color, intensity);
        scene.add(light);

        // --------------------------------main--------------------------------------------

        // 设置相机位置
        camera.position.z = 10
        camera.position.y = 2
        camera.position.x = 2
        camera.lookAt(0, 0, 0)

        // 添加世界坐标辅助器
        const axesHelper = new THREE.AxesHelper(5)
        scene.add(axesHelper)


        // 创建轨道控制器
        const controls = new OrbitControls(camera, renderer.domElement)
        // 阻尼
        controls.enableDamping = true
        // 旋转
        controls.enableRotate = true
        // 缩放
        controls.enableZoom = true
        // 设置阻尼系数
        controls.dampingFactor = 0.05
        // 渲染
        renderer.render(scene, camera)

        // 渲染函数
        function animate() {
            requestAnimationFrame(animate)
            // 更新轨道控制器
            controls.update()
            renderer.render(scene, camera)
            objects.forEach((obj) => {
                obj.position.x += 0.01;
            });
        }
        // 调用渲染函数
        animate()
        // 监听窗口变化
        window.addEventListener('resize', () => {
            // 更新相机
            camera.aspect = window.innerWidth / window.innerHeight
            camera.updateProjectionMatrix()
            // 更新渲染器
            renderer.setSize(window.innerWidth, window.innerHeight)
            renderer.setPixelRatio(window.devicePixelRatio)
        })
    }, [])
    return (
        <>
        </>
    )
}

export default App
